<?php
namespace app\admin\controller;

use app\common\model\Recharge as RechargeModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

class Recharge extends AdminBase
{
    protected $recharge_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->recharge_model = new RechargeModel();
    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($page = 1)
    {
        $recharge_list = $this->recharge_model->order('id DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('index', ['recharge_list' => $recharge_list]);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Recharge');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->recharge_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $recharge = $this->recharge_model->find($id);

        return $this->fetch('edit', ['recharge' => $recharge]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Recharge');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->recharge_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id)
    {
        if ($this->recharge_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function update_status($id = '', $status = '')
    {
        $data['status'] = $status ? 0 : 1;
        if ($this->recharge_model->allowField(true)->where('id',$id)->update($data) !== false) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }

}